<?php


namespace app\admin\business;


use Decimal\Decimal;

class Category
{
    public $model = null;

    public function __construct()
    {
        $this->model = new \app\common\model\mysql\Category();
    }

    public function add($data)
    {
        $data['status'] = 1;
        $this->model->save($data);

        return $this->model->getLastInsID();
    }

    public function getNormalCategories()
    {
        $field = 'id,name,pid';

        $categories = $this->model->getNormalCategories($field);
        if (!$categories) {
            return $categories;
        }
        $categories = $categories->toArray();

        return $categories;
    }

    public function getLists(array $data, int $num)
    {
        $list = $this->model->getLists($data, $num);

        $page = $list->render();
        $res = $list->toArray();

        if (!$res) {
            return [];
        }

        return [$res, $page];
    }

    public function getNormalByPid($pid = 0, $field = "id, name, pid")
    {
        return $this->model->getNormalByPid($pid, $field)->toArray();
    }

    public function getData($cid)
    {
        $obj1 = $this->model->find($cid);
        $obj2 = $this->model->where(['status' => 1, 'id' => $obj1->pid])->find();
        $focus_ids = [];
        $name = '';
        $level2_data = [];
        $level3_data = [];

        //说明是一级分类
        if ($obj1->pid == 0) {
            //二级分类数据
            $level2_data = $this->model->where(['pid' => $cid, 'status' => 1])->field('id , name')->select()->toArray();
            //三级分类数据
            $level3_data = $this->model->where('pid', 'in', array_column($level2_data, 'id'))->where(['status' => 1])->field('id,name')->select()->toArray();

            $name = $obj1->name;
            $focus_ids[0] = $level2_data[0]['id'];
            $focus_ids[1] = $level3_data[0]['id'];
            //说明是二级分类
        } elseif ($obj1->pid != 0 && $obj2->pid == 0) {
            //获取二级数据
            $level2_data = $this->model->where(['pid' => $obj1->pid, 'status' => 1])->field('id , name')->select()->toArray();
            $level3_data = $this->model->where(['pid' => $obj1->id, 'status' => 1])->field('id , name')->select()->toArray();
            $focus_ids[0] = $obj1->id;
            $focus_ids[1] = $level3_data[0]['id'];


            $name = $obj2->name;
            //否则是三级分类
        } else {
            //二级数据的那一个
            $level2 = $this->model->where(['status' => 1, 'id' => $obj1->pid])->find()->toArray();
            $level3_data = $this->model->where(['status' => 1])->where(['pid'=>$obj1->pid])->field('id , name')->select()->toArray();
            //所有的二级数据
            $level2_data = $this->model->where(['status' => 1, 'pid' => $level2['pid']])->field('id , name')->select()->toArray();

            $level1 = $this->model->where(['status' => 1, 'id' => $level2['pid']])->field('id , name')->find()->toArray();
            $name = $level1['name'];


            $focus_ids[0] = $level2['id'];
            $focus_ids[1] = $obj1->id;
        }


        return [
            'name' => $name,
            'focus_ids' => $focus_ids,
            'list' => [
                $level2_data,
                $level3_data,
            ]
        ];

    }

    public function getSubData(int $cid)
    {
        return $level2_data = $this->model->where(['status' => 1, 'pid' => $cid])->select()->toArray();
    }
}